<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Kegiatan
 *
 * @author USER
 */
    class Kegiatan extends CI_Model {
        private $activityID;
        private $activityName;
        private $roomID;
        private $typeID;
        private $day;
        private $startDate;
        private $endDate;
        private $startTime;
        private $endTime;
        private $status;
        private $PJKegiatan;

        public function Kegiatan_model() {
                parent::Model();
        }

        //mengambil satu row dari tabel kegiatan non rutin berdasarkan parameter activityID
        public function getKegiatan($activityID)
        {
            $query = $this->db->get_where('kegiatan',array('activityID'=>$activityID));
            return $query->row();
        }

        public function getAllKegiatan($user_id)
        {
            $query = $this->db->get_where('Kegiatan',array('pjKegiatan'=>$user_id));
            return $query->result();
        }

        //mengambil kegiatan yang sedang dalam status reserved pada $roomID yang merupakan tanggung jawab
        public function getAllKegiatanForConfirmation($roomID)
        {
            $query = $this->db->get_where('Kegiatan',array('roomID'=>$roomID,'status'=>0 ));
            return $query->result();
        }

        //Update by : Arian Kalau status bukan direject.. Keterangan : 0=reserved, 1=approved, 2=?selesai? 3=rejected
        //Return : ruangan yang statusnya bentrok dengan startDate dan endDate,startTime dan endTime (betul kan,Vin?)
        public function getRoomStatus($roomID,$day,$startDate,$endDate,$startTime,$endTime)
        {
            $query = $this->db->query
                    ("
                        SELECT *
                        FROM kegiatan
                        WHERE (roomID = '".$roomID."') AND
                              (day = '".$day."') AND
                              (((startDate <= '".$startDate."') AND ('".$startDate."' <= endDate)) OR
                               ((startDate <= '".$endDate."') AND ('".$endDate."' <= endDate)) OR
                               (('".$startDate."' <= startDate) AND (startDate <= '".$endDate."')) OR
                               (('".$startDate."' <= endDate) AND (endDate <= '".$endDate."'))
                              ) AND
                              (((startTime <= '".$startTime."') AND ('".$startTime."' < endTime)) OR
                               ((startTime < '".$endTime."') AND ('".$endTime."' <= endTime)) OR
                               (('".$startTime."' <= startTime) AND (startTime < '".$endTime."')) OR
                               (('".$startTime."' < endTime) AND (endTime <= '".$endTime."'))
                              )
                              AND
                              (
                                (status=0) OR (status=1)
                              )
                    ");
            return $query->result();
        }

        //Return : ruangan yang statusnya bentrok dengan startDate dan endDate,startTime dan endTime(dimodifikasi tanpa perlu parameter date untuk entry kegiatan non rutin)
        public function getRoomStatusWithoutDay($roomID,$startDate,$endDate,$startTime,$endTime)
        {
            $query = $this->db->query
                    ("
                        SELECT *
                        FROM kegiatan
                        WHERE (roomID = '".$roomID."') AND
                              (((startDate <= '".$startDate."') AND ('".$startDate."' <= endDate)) OR
                               ((startDate <= '".$endDate."') AND ('".$endDate."' <= endDate)) OR
                               (('".$startDate."' <= startDate) AND (startDate <= '".$endDate."')) OR
                               (('".$startDate."' <= endDate) AND (endDate <= '".$endDate."'))
                              ) AND
                              (((startTime <= '".$startTime."') AND ('".$startTime."' < endTime)) OR
                               ((startTime < '".$endTime."') AND ('".$endTime."' <= endTime)) OR
                               (('".$startTime."' <= startTime) AND (startTime < '".$endTime."')) OR
                               (('".$startTime."' < endTime) AND (endTime <= '".$endTime."'))
                              )
                              AND
                              (
                                (status=0) OR (status=1)
                              )
                    ");
            return $query->result();
        }

        public function getJadwal($userID,$day,$startDate,$endDate,$startTime,$endTime)
        {
            //$query=$this->db->query("SELECT * FROM KEGIATAN");
            $query = $this->db->query
                    ("
                        SELECT *
                        FROM kegiatan 
                        WHERE activityID IN
                        (
                            SELECT DISTINCT peserta.activityID
                            FROM peserta
                            WHERE (userID = '".$userID."')
                        ) AND
                            (status = 1) AND
                            (day = '".$day."') AND
                            (((startDate <= '".$startDate."') AND ('".$startDate."' <= endDate)) OR
                             ((startDate <= '".$endDate."') AND ('".$endDate."' <= endDate)) OR
                             (('".$startDate."' <= startDate) AND (startDate <= '".$endDate."')) OR
                             (('".$startDate."' <= endDate) AND (endDate <= '".$endDate."'))
                            ) AND
                            (((startTime <= '".$startTime."') AND ('".$startTime."' < endTime)) OR
                             ((startTime < '".$endTime."') AND ('".$endTime."' <= endTime)) OR
                             (('".$startTime."' <= startTime) AND (startTime < '".$endTime."')) OR
                             (('".$startTime."' < endTime) AND (endTime <= '".$endTime."'))
                            )
                    ");
            return $query->result();
        }

        public function getAllAvailableKegiatan($start,$end)
        {
            $query = $this->db->get('kegiatan');
            return $query->result();
        }

        public function insertKegiatan($activityName,$roomID,$typeID,$day,$startDate,$endDate,$startTime,$endTime,$status,$pjKegiatan)
        {
            $data =array(
                'activityName' =>$activityName,
                'roomID' =>$roomID,
                'typeID' => $typeID,
                'day' =>$day,
                'startDate' =>$startDate,
                'endDate' => $endDate,
                'startTime' => $startTime,
                'endTime' => $endTime,
                'status' => $status,
                'pjKegiatan' => $pjKegiatan
            );
            return $this->db->insert('kegiatan',$data);
        }

        public function updateKegiatan($activityID,$activityName,$roomID,$typeID,$day,$startDate,$endDate,$startTime,$endTime,$status,$pjKegiatan)
        {
            $data =array(
                'activityName' =>$activityName,
                'roomID' =>$roomID,
                'typeID' => $typeID,
                'day' =>$day,
                'startDate' =>$startDate,
                'endDate' => $endDate,
                'startTime' =>$startTime,
                'endTime' => $endTime,
                'status' => $status,
                'pjKegiatan' => $pjKegiatan
            );
            $this->db->where('activityID', $activityID);
            return $this->db->update('kegiatan',$data);
        }

        public function deleteKegiatan($activityID)
        {
            $this->db->where('activityID', $activityID);
            return $this->db->delete('kegiatan');
        }

        //untuk konfirmasi
        public function updateStatusKegiatan($activityID,$status)
        {
            $data =array(
                'status' => $status,
            );
            $this->db->where('activityID', $activityID);
            return $this->db->update('kegiatan',$data);
        }

    }
?>
